<template>
	<view class="container">
		<view class="top">
			<text>浏览历史</text>
			<text class="edit" @click="rightClick">{{rightText}}</text>
		</view>
		<view class="historylist" v-if="historylist.length>0"
			:style="(show&&historylist.length>0)?'padding-bottom:100rpx':'padding-bottom:0px'">
			<view class="history_item" v-for="(item,index) in historylist" :key="index">
				<view class="history_item_text1">{{item.title}}</view>
				<view class="body_bottom_2" v-for="(itm,gindex) in item.child" :key="gindex">
					<view v-if="show" class="body_bottom_left">
						<image @tap="selOne(index,gindex)" class="sel-img" mode="widthFix"
							:src="itm.selected==1?'https://farm-city.oss-cn-hangzhou.aliyuncs.com/static/common/checkbox-sel.png':'https://farm-city.oss-cn-hangzhou.aliyuncs.com/static/common/checkbox.png'" />
					</view>
					<image class="goods_img" :src="itm.goods_img" />
					<view class="body_bottom_2_item">
						<text class="body_bottom_2_text1">{{itm.goods_name}}</text>
						<view class="body_bottom_2_flex">
							<view class="body_bottom_2_flex_tit">￥<text>{{itm.goods_price}}</text></view>
							<image src="https://farm-city.oss-cn-hangzhou.aliyuncs.com/static/icon/delete.png" class="small-img"
								@click="openConfirm(1,index,gindex,itm.visit_id)" />
						</view>
					</view>
				</view>
			</view>
			<u-loadmore :status="hasMore?'loading':'nomore'" />
		</view>
		<view class="empty-container" v-else>
			<u-empty text="暂无浏览历史" :icon="emptyIcon">
				<button @tap='goIndex' size="mini" class="chooseBtn">去逛逛</button>
			</u-empty>
		</view>
		<view class="bottom_settle" v-if="show&&historylist.length>0">
			<view class="bottom_settle_div1" @tap="allSelBtn">
				<image class="bottom_settle_div1-img" mode="widthFix"
					:src="selectAll?'https://farm-city.oss-cn-hangzhou.aliyuncs.com/static/common/checkbox-sel.png':'https://farm-city.oss-cn-hangzhou.aliyuncs.com/static/common/checkbox.png'" />
				<text class="all-text">全选</text>
			</view>
			<button class="del-btn" @tap="openConfirm(2)">删除</button>
		</view>
		<u-popup :safeAreaInsetBottom="false" :safeAreaInsetTop="false" mode="center" :show="showDialog" round="5"
			:overlay="true" :customStyle="customStyle1" @close="showDialog=false">
			<view class="u-popup-slot">
				<text class="u-popup-slot_text">确认删除</text>
				<view class="u-popup-slot_flex">
					<button @click="showDialog = !showDialog" class="cancel">取消</button>
					<button @click="delVisit()" class="confirm">确认</button>
				</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	var _this;
	export default {
		data() {
			return {
				emptyIcon:'https://farm-city.oss-cn-hangzhou.aliyuncs.com/static/common/empty.png',
				show: false,
				selectAll: false,
				pageIndex: 1,
				historylist: [],
				hasMore: true,
				delType: 1, //1单个删除2批量
				showDialog: false,
				customStyle1: {
					'width': '80%'
				},
				parent_index: -1,
				child_index: -1,
				visit_id: 0,
				rightText:'管理'
			}
		},
		onNavigationBarButtonTap(e) {
			// #ifdef H5
			var text = document.getElementsByClassName('uni-btn-icon')[1].innerHTML
			if (text == '管理') {
				if (this.historylist.length == 0) {
					return;
				}
				this.show = true
				document.getElementsByClassName('uni-btn-icon')[1].innerHTML = '完成'
			} else {
				this.show = false
				document.getElementsByClassName('uni-btn-icon')[1].innerHTML = '管理'
			}
			// #endif
			// #ifdef APP-PLUS
			var currentWebview = this.$mp.page.$getAppWebview();
				currentWebview.setTitleNViewButtonStyle(0, {
					text: _this.rightText=='管理'?'完成':'管理'
				});
				if(_this.rightText=='管理'){
					this.show = true
				}else{
					this.show = false
				}
			_this.rightText=_this.rightText=='管理'?'完成':'管理'
			// #endif
		},
		onLoad() {
			_this=this
			this.HistoryList()
		},
		methods: {
			rightClick(){
				if(_this.rightText=='管理'){
					this.show = true
				}else{
					this.show = false
				}
				_this.rightText=_this.rightText=='管理'?'完成':'管理'
			},
			//选择某个商品
			selOne(parent_index, index) {
				var selected = this.historylist[parent_index]['child'][index]['selected']
				this.historylist[parent_index]['child'][index]['selected'] = selected == 1 ? 0 : 1
				this.setSelAll()
			},
			//全选按钮点击
			allSelBtn() {
				if (this.historylist.length == 0) {
					return
				}
				this.selectAll = !this.selectAll
				for (var i = 0; i < this.historylist.length; i++) {
					var child = this.historylist[i]['child']
					for (var j = 0; j < child.length; j++) {
						child[j]['selected'] = this.selectAll ? 1 : 0
					}
				}
			},
			//设置全选按钮
			setSelAll() {
				var selectAll = true
				for (var i = 0; i < this.historylist.length; i++) {
					var child = this.historylist[i]['child']
					for (var j = 0; j < child.length; j++) {
						if (child[j]['selected'] == 0) {
							selectAll = false
							break
						}
					}
				}
				this.selectAll = selectAll
			},
			//加入购物车
			async addcart(id) {
				const params = {
					goods_id: id,
					item_id: 0,
					goods_num: 1
				}
				const data = await AddCart(params)
				if (data.code == 200) {
					this.$u.toast(data.msg)
				} else {
					this.$u.toast(data.msg)
				}
			},
			//打开确认删除对话框
			openConfirm(type, parent_index, index, visit_id) {
				if (type == 1) {
					this.parent_index = parent_index
					this.child_index = index
					this.visit_id = visit_id
				} else {
					var ids = this.getSelIds();
					if (ids == 0) {
						uni.$u.toast('请选择商品')
						return false
					}
				}
				this.delType = type
				this.showDialog = true
			},
			//获取选中的ids
			getSelIds() {
				var historylist = this.historylist;
				var haveSel = [];
				for (var i = 0; i < historylist.length; i++) {
					var child = historylist[i]['child']
					for (var j = 0; j < child.length; j++) {
						if (child[j]['selected'] == 1) {
							haveSel.push(child[j]['visit_id'])
						}
					}
				}
				let SelIds = haveSel.length > 0 ? haveSel.join(',') : 0;
				return SelIds;
			},
			async HistoryList() {
				const params = {
					pageIndex: this.pageIndex,
					pageSize: 10,
				}
				const data = await _this.$post('api/VisitList',params)
				this.historylist.push(...data.data.list)
				this.hasMore = data.data.list.length >= 10
			},
			//触底事件
			onReachBottom() {
				//重新请求数据，带上分页的数据
				if (this.hasMore) {
					this.pageIndex++
					this.HistoryList()
				}
			},
			//删除浏览历史
			async delVisit() {
				if(this.delType==1){
					var params = {
						ids: this.visit_id+''
					}
				}else{
					var ids=this.getSelIds()
					var params = {
						ids: ids
					}
				}
				const data = await _this.$post('api/DelVisit',params)
				if(data.code==200){
					uni.$u.toast(data.msg)
					if(this.delType==1){
						this.historylist[this.parent_index]['child'].splice(this.child_index,1)
					}else{
						this.historylist=[]
						this.HistoryList()
					}
				}else{
					uni.$u.toast(data.msg)
				}
				this.showDialog = false
			}
		}
	}
</script>

<style lang="scss" scoped>
	.container {
		padding: 0 20rpx 0 20rpx;
	}
	.top{
		display: flex;
		align-items: center;
		text-align: right;
		justify-content: space-between;
		text{
			line-height: 80rpx;
		}
		.edit{
			padding: 0 0 0 30rpx;
		}
	}
	.historylist {
		.history_item {
			.history_item_text1 {
				color: $color-919191;
				height: 60rpx;
				display: flex;
				align-items: center;
			}

			.body_bottom_2 {
				display: flex;
				margin-bottom: 20rpx;
				padding: 30rpx;
				background: #FFFFFF;
				border-radius: 20rpx;

				.body_bottom_left {
					margin-right: 20rpx;
					align-self: center;

					.sel-img {
						width: 40rpx;
					}
				}

				.goods_img {
					width: 140rpx;
					height: 140rpx;
					border-radius: 10rpx;
				}

				.body_bottom_2_item {
					flex: 1;
					display: flex;
					flex-direction: column;
					margin-left: 20rpx;
					justify-content: space-between;
					padding: 10rpx 0;

					.body_bottom_2_text1 {
						font-size: 28rpx;
					}

					.body_bottom_2_flex {
						width: 100%;
						display: flex;
						align-items: center;

						.body_bottom_2_flex_tit {
							color:$theme-F94A36;
							flex: 1;
							font-size: 26rpx;
							text {
								font-size: 30rpx;
							}
						}

						.small-img {
							width: 30rpx;
							height: 30rpx;

							&:not(:last-child) {
								margin-right: 10rpx;
							}
						}
					}
				}
			}
		}
	}

	.u-empty-container {
		padding-top: 20%;

		.chooseBtn {
			color: #fff;
			margin-top: 20rpx;
			background: $theme-color;
		}
	}

	.bottom_settle {
		display: flex;
		align-items: center;
		position: fixed;
		bottom: 0;
		left: 0;
		height: 100rpx;
		width: 100%;
		padding: 0 20rpx;
		box-sizing: border-box;
		background: #FFFFFF;

		.bottom_settle_div1 {
			display: flex;
			align-items: center;
			flex: 1;

			.bottom_settle_div1-img {
				width: 40rpx;
			}

			.all-text {
				margin-left: 10rpx;
				font-size: 30rpx;
			}
		}

		.del-btn {
			width: 180rpx;
			height: 70rpx;
			line-height: 70rpx;
			border-radius: 40rpx;
			background-color: $theme-color;
			font-size: 30rpx;
			color: #fff;
		}
	}
</style>
